package com.baizhi.interfacex2;

public class AnimalDemo {
    public static void main(String[] args) {
        Pet[] pets = {
               new Dog("旺财",3,"黄"),
                new Cat("咪咪",2,"花"),
                new Cat("喵喵",1,"白"),
                new Bird("Bili",1,"粉红"),
                new Fish("小金",1,"金"),
                new Bird("飞飞",1,"蓝"),
                new Fish("小银",1,"银"),
        };

        //我想看我的动物表演他们的技能， 猫狗会跑，  鱼会游泳，  鸟会飞

        for (int i = 0; i < pets.length; i++) {
            //获取每一个宠物
            Pet pet = pets[i];
            //pet 属于Pet类型的，但是pets[i] 具体是Dog,还是Cat,还是Bird在编译的时候无法确认，
            //所以可以通过instanceof 判断，它是什么类型的，也可以判断pet的具体对象（Dog,Cat，Bird） 是否实现了某个接口
            if(pet instanceof Flying){ //判断pet对象是否实现了FLying接口
                pet.eat(); //pet是Pet类型的，eat属于Pet类，所以可以直接调用
                    //找出所有会飞的宠物
                Flying f = (Flying)pet;
                f.fly();

            }else if(pet instanceof Swiming){
                pet.eat();
                //找出会游泳的宠物
                Swiming s = (Swiming) pet;
                s.swim();

            }else if(pet instanceof  Running){
                pet.eat();
                Running r = (Running)pet;
                r.run();

            }
        }
    }
}
